package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.ICompanyService;
import redis.clients.jedis.Jedis;

/**
 * 逻辑层 服务层
 */
public class CompanyServiceImpl implements ICompanyService {
    /**
     * 1.首先判断数据来源
     * 2.如果缓存有数据，则取缓存
     * 3.如果没有数据则取数据库
     *
     * @return
     */
    @Override
    public CompanyModel getCompanyModel() {
        final CompanyModel[] companyModel = {new CompanyModel()};//常量
        for (int i = 0; i < 500; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    companyModel[0] = getData();
                    System.out.println(Thread.currentThread().getName());
                }
            }, "线程" + i + "正在执行").start();
        }


        return companyModel[0];
    }

    private synchronized CompanyModel getData() {
        Jedis jedis = new Jedis();
        CompanyModel companyModel = new CompanyModel();
        boolean boo = jedis.exists("companyId");//缓存里面是否有数据
        if (boo) {
// 缓存里面的数据
            companyModel.setId(Integer.parseInt(jedis.get("companyId")));
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setNet(jedis.get("net"));
            companyModel.setEmail(jedis.get("email"));
            companyModel.setLinePhone(jedis.get("linePhone"));
            companyModel.setAddress(jedis.get("address"));
            companyModel.setVersion(jedis.get("version"));
            System.out.println("redis");
        } else {
            CompanyDao companyDao = new CompanyImpl();
            companyModel = companyDao.getCompanyModel();
            //需要将查询出来的数据存入缓存,用string
            jedis.set("companyId", companyModel.getId() + "");
            jedis.setex("logo", 1000, companyModel.getLogo());
            jedis.set("phone", companyModel.getPhone());
            jedis.set("net", companyModel.getNet());
            jedis.set("email", companyModel.getEmail());
            jedis.set("phone", companyModel.getLinePhone());
            jedis.set("address", companyModel.getAddress());
            jedis.set("version", companyModel.getVersion());
            jedis.set("linePhone", companyModel.getLinePhone());
            System.out.println("mysql");
        }
        return companyModel;
    }
/**
 * 1.下载jar包
 * 2.在服务逻辑层取判断
 * 3.jedis   set   get   setex exists  expire  incr  decr
 * 4:注意地方：每一次做更新操作时都需要删除id。
 */
//    public int update(CompanyModel companyModel) {
//        CompanyDao companyDao = new CompanyImpl();
//        int result = companyDao.update(companyModel);
//        if (result == 1) {
//            Jedis jedis = new Jedis();
//            jedis.del("companyId");
//        }
//        return result;
//    }
//
//    public int add(CompanyModel companyModel) {
//        CompanyDao companyDao = new CompanyImpl();
//        int result = companyDao.add(companyModel);
//        if (result == 1) {
//            Jedis jedis = new Jedis();
//            jedis.del("companyId");
//        }
//        return 0;
//    }
//
//    public int del(CompanyModel companyModel) {
//        CompanyDao companyDao = new CompanyImpl();
//        int result = companyDao.del(companyModel);
//        if (result == 1) {
//            Jedis jedis = new Jedis();
//            jedis.del("companyId");
//        }
//        return 0;
//    }
}
